
package com.chuanke.ckfamily.action.social;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chuanke.ckfamily.action.BaseController;
import com.chuanke.ckfamily.action.base.Response;
import com.chuanke.ckfamily.dao.base.Page;
import com.chuanke.ckfamily.dao.social.model.Family;
import com.chuanke.ckfamily.dao.social.model.FamilyMember;
import com.chuanke.ckfamily.dao.user.model.User;
import com.chuanke.ckfamily.service.base.FailCode;
import com.chuanke.ckfamily.service.common.CacheService;
import com.chuanke.ckfamily.service.common.TipsService;
import com.chuanke.ckfamily.service.common.impl.RedisLockUtil;
import com.chuanke.ckfamily.service.exception.CFException;
import com.chuanke.ckfamily.service.social.FamilyMemberService;
import com.chuanke.ckfamily.service.social.FamilyService;
import com.chuanke.ckfamily.service.user.UserService;

@Api(tags = "家族相关接口")
@Controller
@RequestMapping("/api/family")
public class FamilyController extends BaseController {

	private static final Logger log = LoggerFactory.getLogger(FamilyController.class);
	
	@Autowired
	private FamilyService familyService;

	@Autowired
	private FamilyMemberService familyMemberService;
	
	@Autowired
	private TipsService tipsService;
	
    @Autowired
    private UserService userService;

    @Autowired
    private CacheService cacheService;
    
	/**
	 * 
	 * 我的家族
	 * api/family/myFamily
	 * @return
	 */
    @ApiOperation(value = "我的家族",httpMethod="POST")
    @ResponseBody
    @RequestMapping("/myFamily")
    public Response myFamily(HttpServletRequest request) {
		try {
			Long uid = getUidByToken(request);
			Map<String, Object> result = familyService.myFamily(uid);
			return new Response(result);
		} catch (Exception e) {
			e.printStackTrace();
			 return new Response(FailCode.APP);
		}
    }

    /**
     * 粉丝家族
	 * api/family/fansFamily
     * @param p  分页 1开始
     * @param keyword 
     * @return
     */
    @ApiOperation(value = "粉丝家族",httpMethod="POST")
	@ApiImplicitParams({
		    @ApiImplicitParam(name = "P", value = "分页", required = false, paramType="form",dataTypeClass=Integer.class),
		    @ApiImplicitParam(name = "keyword", value = "搜索关键字", required = false,paramType="form", dataTypeClass=String.class)
	})
    @ResponseBody
    @RequestMapping("/fansFamily")
	public Response fansFamily(HttpServletRequest request,
			@RequestParam(value="p",required=false) Integer p,
			@RequestParam(value="keyword",required=false) String keyword) {
		try {
			Long uid = getUidByToken(request);
			Map<String, Object> result = familyService.fansFamily(uid,keyword,p);
			logger.debug("get fans family===========> uid={},keyword={},result={}",uid,keyword,result);
			return new Response(result);

		} catch (Exception e) {
			e.printStackTrace();
			 return new Response(FailCode.APP);

		}
    }

	/**
	 * 查看自己群信息
	 * api/family/my_family_info
	 * @param request
	 * @return
	 */
    @ApiOperation(value = "查询当前用户家族信息",httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "tid", value = "家族tid", required = false, paramType="form",dataTypeClass=String.class)
    })
	@RequestMapping("my_family_info")
	@ResponseBody
    public Response myFamilyInfo(HttpServletRequest request,@RequestParam(value = "tid", required = true) String tid) {
    	try {
    		Long uid = getUidByToken(request);
			Map<String, Object> familyInfo = familyService.familyInfo(uid, null,tid);
			familyInfo.put("helpCenter", tipsService.getHelpUrl());
			logger.debug("my_family_info==========>uid={},tid={}",uid,tid);
			return new Response(familyInfo);

		} catch (CFException e) {
    		return new Response(e);

		} catch (Exception e) {
    		e.printStackTrace();
    		 return new Response(FailCode.APP);

		}
	}
    
	/**
	 *
	 * 创建家族
	 * api/family/createFamily
	 * @param icon 家族头像
	 * @param familyName 家族名称
	 * @return
	 */
    @ApiOperation(value = "创建家族",notes="",httpMethod="POST")
   	@ApiImplicitParams({
   		    @ApiImplicitParam(name = "familyName", value = "家族名称", required = true,paramType="form", dataTypeClass=String.class),
   		    @ApiImplicitParam(name = "icon", value = "家族头像", required = true, paramType="form",dataTypeClass=String.class)
   	})
	@ResponseBody
	@RequestMapping("/createFamily")
	public Response createFamily(HttpServletRequest request,
			@RequestParam("familyName") String familyName,
			@RequestParam(value = "icon", required = true) String icon) {
		RedisLockUtil lockUtil = null;
		try {
			Long uid = getUidByToken(request);
			lockUtil = cacheService.getLock(CacheService.KEY_PREFIX.move_user_.name() + uid);

    		Family result = familyService.createFamily(uid, familyName, icon);
    		if(result!=null){
    			 String leader_know = tipsService.getLeader_know();
    			 result.setCreate_family_tips(leader_know);
    			 Response response = new  Response(true);
    			 response.setData(result);
    			return response;

    		}else{
    			Response response = new  Response(false);
    			return response;

    		}
    		
		}  catch (CFException e) {
            return new Response(e);

        } catch (Exception e) {
            e.printStackTrace();
            return new Response(FailCode.APP);

        } finally {
			if (lockUtil != null) {
				lockUtil.unlock();
			}

		}
	}

	/**
	 * 
	 * 家族资料
	 * api/family/data
	 * @param familyId 家族id
	 * @return
	 */
    @ApiOperation(value = "家族资料",notes="familyId不为为空时查询指定用户家族资料，否则查询当前用户所在家族资料",httpMethod="POST")
   	@ApiImplicitParams({
   		    @ApiImplicitParam(name = "familyId", value = "家族ID", required =false, paramType="form",dataTypeClass=Long.class)
   	})
    @ResponseBody
    @RequestMapping("/data")
	public Response familyData(HttpServletRequest request,
			@RequestParam(value = "familyId", required = false) Long familyId) {
    	try {
    		Long uid = getUidByToken(request);
			Map<String, Object> result = familyService.familyInfo(uid, familyId,null);
			logger.debug("data==========>uid={},familyId={}",uid,familyId);
			return new  Response(result);

    	} catch (CFException e) {
            return new Response(e);

        }catch (Exception e) {
            e.printStackTrace();
            return new Response(FailCode.APP);

        }
    }
    
    /**
	 * 
	 * 修改家族信息
	 * api/family/familyUpdate
	 * @param announcement 公告
	 * @param icon 家族头像
	 * @param familyName 家族名称
	 * @return
	 */
    @ApiOperation(value = "修改家族信息",httpMethod="POST")
   	@ApiImplicitParams({
   		    @ApiImplicitParam(name = "announcement", value = "家族公告", required =false,paramType="form", dataTypeClass=String.class),
   		    @ApiImplicitParam(name = "familyName", value = "家族名称", required = false,paramType="form", dataTypeClass=String.class),
 		    @ApiImplicitParam(name = "icon", value = "家族头像", required = false,paramType="form", dataTypeClass=String.class),
   		    @ApiImplicitParam(name = "tid", value = "家族tid", required =true,paramType="form", dataTypeClass=String.class)
   	})
    @ResponseBody
    @RequestMapping("/familyUpdate")
    public Response familyUpdate(
    		HttpServletRequest request,
    		@RequestParam(value="announcement" ,required=false)String announcement,
    		@RequestParam(value="icon" ,required=false)String icon,
    		@RequestParam(value="familyName" ,required=false)String familyName,
    		@RequestParam(value = "tid", required = true) String tid) {
    	try {
    		Long uid = getUidByToken(request);
    		logger.debug("familyUpdate======>uid={},announcement={},icon={},familyName={}",uid,announcement,icon,familyName);

    		familyService.familyUpdate(uid,tid, announcement, icon, familyName);
    		Map<String, Object> myFamily = familyService.myFamily(uid);
			return new  Response(myFamily);

		}  catch (CFException e) {
            return new Response(e);

        } catch (Exception e) {
            e.printStackTrace();
            return new Response(FailCode.APP);
        }
    }
    
    
    /**
     * 家族消息免打扰
	 * api/family/family_nocall
     * @param  nocall 是否消息免打扰。0：关闭消息免打扰 1：打开消息免打扰
     * @return
     */
    @ApiOperation(value = "家族消息免打扰",httpMethod="POST")
   	@ApiImplicitParams({
   		    @ApiImplicitParam(name = "nocall", value = "是否消息免打扰。0：关闭消息免打扰 1：打开消息免打扰", required =true,paramType="form", dataTypeClass=Integer.class),
   		    @ApiImplicitParam(name = "tid", value = "家族tid", required =true,paramType="form", dataTypeClass=String.class)
   	})
    @ResponseBody
    @RequestMapping("/family_nocall")
    public Response family_nocall(
    		HttpServletRequest request,
    		@RequestParam("nocall")int nocall,
    		@RequestParam(value = "tid", required = true) String tid) {
		RedisLockUtil lockUtil = null;
		try {
			Long uid = getUidByToken(request);
			lockUtil = cacheService.getLock(CacheService.KEY_PREFIX.familyMember_.name()+ uid);
    		Map<String, Object>  result=familyService.familyNocall(uid, nocall, tid);
    		logger.debug("family_nocall==========>uid={},nocall={},tid={}",uid,nocall,tid);
			return new  Response(result);

		}  catch (CFException e) {
            return new Response(e);

        } catch (Exception e) {
            e.printStackTrace();
            return new Response(FailCode.APP);
        }finally{
        	if(lockUtil!=null){
        		lockUtil.unlock();
        	}
        }
    }
    
    /**
     * 退出家族的功能
	 * api/family/leaveFamily
     * @return
     */
    @ApiOperation(value = "退出家族",httpMethod="POST")
   	@ApiImplicitParams({
   		    @ApiImplicitParam(name = "tid", value = "家族tid", required =true,paramType="form", dataTypeClass=String.class)
   	})
    @ResponseBody
    @RequestMapping("/leaveFamily")
	public Response leaveFamily(HttpServletRequest request,
			@RequestParam(value = "tid", required = true) String tid) {
		RedisLockUtil lockUtil = null;
		try {
			Map<String, Object> result=new HashMap<>();
    		Long uid = getUidByToken(request);
			lockUtil = cacheService.getLock(CacheService.KEY_PREFIX.familyLock.name() + tid);

    		boolean leave=familyMemberService.leaveFamily(uid,false, tid);
    		
    		FamilyMember member = familyMemberService.findByUserId(uid);
    		result.put("shield", member!=null ? member.getShield() : 0);
    			
    		logger.debug("leaveFamily==========>uid={},tid={}",uid,tid);
    		Response response = new  Response(result);
    		if(leave){
    			response.setSuccess(true);
    			response.setMsg("退出成功");
    		}else{
    			response.setSuccess(false);
    			response.setMsg("退出失败");
    		}
			return response;

		}  catch (CFException e) {
            return new Response(e);

        } catch (Exception e) {
            e.printStackTrace();
            return new Response(FailCode.APP);

        } finally {
			if (lockUtil != null) {
				lockUtil.unlock();
			}

		}
	}
    
    
    /**
     * 查询没有活跃的家族成员
     * api/family/can_delete_member
     * @param p 分页
     * @param keyword 搜索关键字
     * @param tid 
     * @param request
     * @return
     */
    @ApiOperation(value = "查询没有活跃的家族成员",httpMethod="POST")
   	@ApiImplicitParams({
		   	@ApiImplicitParam(name = "p", value = "分页", required =false,paramType="form", dataTypeClass=Integer.class),
		   	@ApiImplicitParam(name = "keyword", value = "搜索关键字", required =false,paramType="form", dataTypeClass=String.class),
   		    @ApiImplicitParam(name = "tid", value = "家族tid", required =true,paramType="form", dataTypeClass=String.class)
   	})
    @ResponseBody
    @RequestMapping("/can_delete_member")
    public Response can_delete_member(HttpServletRequest request,
    		@RequestParam(value="p",required=false) Integer p,
    		@RequestParam(value="keyword",required=false) String keyword,
    		@RequestParam(value = "tid", required = true) String tid) {
    	try {
    		Long uid = getUidByToken(request);
    	
    		List<User> result=familyService.getCanDeleteMember(uid,keyword,p,tid);
    		logger.debug("can_delete_member==========>uid={},p={},keyword={},tid={}",uid,p,keyword,tid);
    		formatCanAddMember(result);
			Response response = new Response(result);
			return response;
			
		}  catch (CFException e) {
            return new Response(e);

        } catch (Exception e) {
            e.printStackTrace();
            return new Response(FailCode.APP);
        }
    }

    /**
     * 族长踢人的功能
     * api/family/kickMember
     * @param tuserIds 用户id 踢出多个成员是，使用英文逗号分隔
     * @return
     */
    @ApiOperation(value = "族长踢人",httpMethod="POST")
   	@ApiImplicitParams({
		   	@ApiImplicitParam(name = "tuserIds", value = "用户id，多个成员时使用英文逗号分隔", required =false, paramType="form",dataTypeClass=String.class),
   		    @ApiImplicitParam(name = "tid", value = "家族tid", required =true, paramType="form",dataTypeClass=String.class)
   	})
    @ResponseBody
    @RequestMapping("/kickMember")
	public Response kickMember(HttpServletRequest request,
			@RequestParam("tuserIds") String tuserIds,
			@RequestParam(value = "tid", required = true) String tid) {
    	RedisLockUtil lockUtil = null;
    	List<String> list = null;
		try {
			Long uid = getUidByToken(request);
			if (tuserIds != null) {
				String[] split = tuserIds.split(",");
				list = Arrays.asList(split);
			}
			lockUtil = cacheService.getLock(CacheService.KEY_PREFIX.familyLock.name() + tid + "_" + uid);
			int result = familyMemberService.kickFamilyMember(uid, list, tid);
			logger.debug("kickMember==========>uid={},tuserIds={},tid={},result={}", uid, tuserIds, tid, result);
			if (result > 0) {
				Response response = new Response(result);
				response.setMsg("踢出成功");
				return response;

			} else {
				Response response = new Response(result);
				response.setSuccess(false);
				response.setMsg("踢出失败");
				return response;
			}

		} catch (CFException e) {
			return new Response(e);

		} catch (Exception e) {
			e.printStackTrace();
			return new Response(FailCode.APP);
		}finally{
			if(lockUtil!=null){
				lockUtil.unlock();
			}
		}
	}
    
    /**
     * 
     * 解除禁言
	 * api/family/unMute
     * @param  tuserId 被解除禁言用户ID
     * @param  tid 
     * @return
     */
    @ApiOperation(value = "解除禁言",httpMethod="POST")
   	@ApiImplicitParams({
		   	@ApiImplicitParam(name = "tuserIds", value = "被解除禁言用户ID", required =false, paramType="form",dataTypeClass=Long.class),
   		    @ApiImplicitParam(name = "tid", value = "家族tid", required =true, paramType="form",dataTypeClass=String.class)
   	})
    @ResponseBody
    @RequestMapping("/unMute")
	public Response unMute(HttpServletRequest request,
			@RequestParam("tuserId") Long tuserId,
			@RequestParam(value = "tid", required = true) String tid) {
    	RedisLockUtil lockUtil = null;
		try {
			Long uid = getUidByToken(request);
			lockUtil = cacheService.getLock(CacheService.KEY_PREFIX.familyMember_.name()+ uid);
			Map<String, Object> result = familyMemberService.unMuteFamilyMember(uid, tuserId, tid);
			logger.debug("unMute==========>uid={},tuserId={},tid={}", uid, tid, result);
			return new Response(result);

		} catch (CFException e) {
			return new Response(e);

		} catch (Exception e) {
			e.printStackTrace();
			return new Response(FailCode.APP);
		}finally{
			if(lockUtil!=null){
				lockUtil.unlock();
			}
		}
	}
    
    /**
     * 禁言
	 * api/family/mute
     * @param  tuserId 待禁言用户ID
     * @param  tid 
     * @return
     */
    @ApiOperation(value = "禁言",httpMethod="POST")
   	@ApiImplicitParams({
		   	@ApiImplicitParam(name = "tuserId", value = "待禁言用户ID", required =false, paramType="form",dataTypeClass=Long.class),
   		    @ApiImplicitParam(name = "tid", value = "家族tid", required =true,paramType="form",dataTypeClass=String.class)
   	})
    @ResponseBody
    @RequestMapping("/mute")
	public Response mute(HttpServletRequest request,
			@RequestParam("tuserId") Long tuserId,
			@RequestParam(value = "tid", required = true) String tid) {
		RedisLockUtil lockUtil = null;
		try {
			Long uid = getUidByToken(request);
			lockUtil = cacheService.getLock(CacheService.KEY_PREFIX.familyMember_.name()+ uid);
			Map<String, Object> result = familyMemberService.muteFamilyMember(uid, tuserId, tid);
			logger.debug("mute======>uid={},tuserId={},tid={},result={}", uid, tuserId, tid, result);
			return new Response(result);

		} catch (CFException e) {
			return new Response(e);

		} catch (Exception e) {
			e.printStackTrace();
			return new Response(FailCode.APP);
		}finally{
			if(lockUtil!=null){
				lockUtil.unlock();
			}
		}
	}

    /**
     * 家族成员
	 * api/family/familyMember
     * @param nickName 
     * @param sort 排序 1：默认 2：活跃 3贡献 4.时间
     * @param order 排序 1.升序 2.降序 
     * @param p 分页。0开始。
     * @param date 默认排序 时间戳
     * @return
     */
    @ApiOperation(value = "查询家族成员",httpMethod="POST")
   	@ApiImplicitParams({
		   	@ApiImplicitParam(name = "nickName", value = "搜索关键字", required =false,paramType="form", dataTypeClass=String.class),
			@ApiImplicitParam(name = "sort", value = "排序 1：默认 2：活跃 3贡献 4.时间", required =false,paramType="form", dataTypeClass=Integer.class),
			@ApiImplicitParam(name = "order", value = "排序 1.升序 2.降序 ", required =false,paramType="form", dataTypeClass=Integer.class),
			@ApiImplicitParam(name = "p", value = "分页", required =false, paramType="form",dataTypeClass=Integer.class),
			@ApiImplicitParam(name = "date", value = "默认排序 时间戳", required =false,paramType="form", dataTypeClass=Long.class),
   		    @ApiImplicitParam(name = "tid", value = "家族tid", required =true, paramType="form",dataTypeClass=String.class)
   	})
    @ResponseBody
    @RequestMapping("/familyMember")
	public Response familyMember(
			HttpServletRequest request,
			@RequestParam(value = "nickName", required = false) String nickName,
			@RequestParam(value="sort",required=true) int sort,
			@RequestParam(value = "order", required = false, defaultValue = "2") Integer order,
			@RequestParam(value = "p", required = false) Integer p,
			@RequestParam(value = "date", required = false) Long date,
			@RequestParam(value = "tid", required = true) String tid) {
		try {
			Long uid = getUidByToken(request);
			Map<String, Object> result = familyService.familyMember(uid, nickName, sort, order, date, p, tid);
			logger.debug("familyMember==========>uid={},sort={},order={},p={},date={},tid={},result={}", uid, sort, order, p, date, tid,result);
			return new Response(result);
		} catch (CFException e) {
			return new Response(e);

		} catch (Exception e) {
			e.printStackTrace();
			return new Response(FailCode.APP);
		}

	}
    
    /**
     * 查询待邀请粉丝加入家族
	 * api/family/requestMember
     * @param nickName 昵称
     * @param p 分页 1开始
     * @return
     */
    @ApiOperation(value = "查询待邀请粉丝加入家族",httpMethod="POST")
   	@ApiImplicitParams({
		   	@ApiImplicitParam(name = "nickName", value = "搜索关键字", required =false,paramType="form", dataTypeClass=String.class),
			@ApiImplicitParam(name = "p", value = "分页", required =false,paramType="form", dataTypeClass=Integer.class),
   		    @ApiImplicitParam(name = "tid", value = "家族tid", required =true, paramType="form",dataTypeClass=String.class)
   	})
    @ResponseBody
    @RequestMapping("/requestMember")
	public Response requestMember(
			HttpServletRequest request,
			@RequestParam(value = "nickName", required = false) String nickName,
			@RequestParam(value="p",required=false) Integer p,
			@RequestParam(value = "tid", required = true) String tid) {
    	try {
    		Long uid = getUidByToken(request);
			Page page = p != null ? new Page(p) : null;
    		List<User> result = familyMemberService.getCanAddMember(uid, nickName,page,tid);
    		formatCanAddMember(result);
    		logger.debug("requestMember==========>uid={},nickName={},p={},tid={},result={}",uid,nickName,p,tid,result);
			return new  Response(result);

		}  catch (CFException e) {
            return new Response(e);

        } catch (Exception e) {
            e.printStackTrace();
            return new Response(FailCode.APP);

        }
    }
    
    private void formatCanAddMember(List<User> result) {
    	if(result==null || result.isEmpty()){
    		return;
    	}
		for (User user : result) {
			user.setReal_name(user.getNick_name());
		}
		
	}

	/**
     * 邀请粉丝加入家族
	 * api/family/addFamilyMember
	 * @param onekey：是否一键邀请粉丝  true:是  false：否  （onekey 为false需要tuserIds参数，否则不需要）
     * @param tuserIds 邀请粉丝id 多个此参数时 英文逗号分隔
     */
    @ApiOperation(value = "邀请粉丝加入家族",httpMethod="POST")
   	@ApiImplicitParams({
		   	@ApiImplicitParam(name = "tuserIds", value = "邀请粉丝id，邀请多个此粉丝时英文逗号分隔", required =false,paramType="form", dataTypeClass=String.class),
			@ApiImplicitParam(name = "onekey", value = "是否一键邀请粉丝 ,true:是  false：否 。 为false时需要tuserIds参数，否则不需要", required =false, paramType="form",dataTypeClass=Boolean.class),
   		    @ApiImplicitParam(name = "tid", value = "家族tid", required =true, paramType="form",dataTypeClass=String.class)
   	})
    @ResponseBody
    @RequestMapping("/addFamilyMember")
	public Response addFamilyMember(
			HttpServletRequest request,
			@RequestParam(value = "tuserIds", required = false) String tuserIds,
			@RequestParam(value = "onekey", required = false) Boolean onekey,
			@RequestParam(value = "tid", required = true) String tid) {
    	
    	RedisLockUtil lockUtil = null;
    	List<String> list = null;
		try {
			if (tuserIds != null) {
				String[] split = tuserIds.split(",");
				list = Arrays.asList(split);
			}
			
			Long uid = getUidByToken(request);
			lockUtil = cacheService.getLock(CacheService.KEY_PREFIX.familyLock.name() + tid + "_" + uid);
			int num = familyMemberService.addFamilyMembers(uid, list, onekey, tid);

			Map<String, Object> result = new HashMap<>();
			result.put("num", num);
			Response response = new Response(result);
			if (num <= 0) {
				response.setSuccess(false);
				response.setMsg("邀请失败");
			}

			return response;

		} catch (CFException e) {
			return new Response(e);

		} catch (Exception e) {
			e.printStackTrace();
			return new Response(FailCode.APP);
		}finally{
			if (lockUtil != null) {
				lockUtil.unlock();
			}
		}
	}
    
    /**
     * 家族收益
	 * api/family/income
     * @return
     */
    @ApiOperation(value = "家族收益",httpMethod="POST")
   	@ApiImplicitParams({
   		    @ApiImplicitParam(name = "tid", value = "家族tid", required =true,paramType="form",dataTypeClass=String.class)
   	})
    @ResponseBody
    @RequestMapping("/income")
    public Response income(HttpServletRequest request,
    		@RequestParam(value = "tid", required = true) String tid) {
		try {
			Long uid = getUidByToken(request);
			Map<String, Object> result = familyService.income(uid, tid);
			logger.debug("income==========>uid={},tid={},result={}", uid, tid, result);
			return new Response(result);

		} catch (CFException e) {
			return new Response(e);

		} catch (Exception e) {
			e.printStackTrace();
			return new Response(FailCode.APP);
		}
	}
    
    
    /**
     * 
     * 更新并且返回家族公告已读数量
	 * api/family/announcement_read
     * @return
     */
    @ApiOperation(value = "更新并且返回家族公告已读数量",httpMethod="POST")
   	@ApiImplicitParams({
   		    @ApiImplicitParam(name = "tid", value = "家族tid", required =true, paramType="form",dataTypeClass=String.class)
   	})
    @ResponseBody
    @RequestMapping("/announcement_read")
    public Response announcement_read(HttpServletRequest request,@RequestParam(value = "tid", required = true) String tid) {
		try {
			Long uid = getUidByToken(request);
			int num = familyService.announcement_read(uid, tid);
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("num", num);
			logger.debug("announcement_read==========>uid={},tid={},result={}", uid, tid, result);
			return new Response(result);

		} catch (CFException e) {
			return new Response(e);

		} catch (Exception e) {
			e.printStackTrace();
			return new Response(FailCode.APP);
		}
	}
    
    /**
     * 
     * 家族会话初始化查询接口。成员数量，成员退家族提示，分享红包提示 ,是否屏蔽消息
	 * api/family/   notice
	 * memberCount 当前家族成员数量
	 * leave_notice 是否显示退家族提示 。false:否 ，true：是
	 * packet_notice 是否显示分享领红包提示 。false:否 ，true：是
     * @return
     */
    @ApiOperation(value = "家族会话初始化查询接口",httpMethod="POST")
   	@ApiImplicitParams({
   		    @ApiImplicitParam(name = "tid", value = "家族tid", required =true,paramType="form", dataTypeClass=String.class)
   	})
    @ResponseBody
    @RequestMapping("/notice")
    public Response family_notice(HttpServletRequest request,@RequestParam(value = "tid", required = false) String tid) {
		try {
			Long uid = getUidByToken(request);
			Map<String, Object> result = familyService.getFamilyNotice(uid, tid);
			logger.debug("notice==========>uid={},tid={},result={}", uid, tid, result);
			return new Response(result);

		} catch (CFException e) {
			return new Response(e);

		} catch (Exception e) {
			e.printStackTrace();
			return new Response(FailCode.APP);
		}
	}
    
    /**
     * 取消退家族，领红包提示
	 * api/family/ignore_notice
	 * @param type 1退家族 2：分享领红包
     * @return
     */
    @ApiOperation(value = "取消退家族，领红包提示",httpMethod="POST")
   	@ApiImplicitParams({
   		    @ApiImplicitParam(name = "type", value = "1退家族 2：分享领红包", required =true,paramType="form", dataTypeClass=Integer.class),
   		    @ApiImplicitParam(name = "tid", value = "家族tid", required =true, paramType="form",dataTypeClass=String.class)
   	})
    @ResponseBody
    @RequestMapping("/ignore_notice")
	public Response ignore_notice(HttpServletRequest request,
			@RequestParam("type") int type,
			@RequestParam(value = "tid", required = false) String tid) {
		try {
			Long uid = getUidByToken(request);
			boolean result = familyService.ignoreNotice(uid, type, tid);
			logger.debug("ignore_notice==========>uid={},type={},tid={},result={}", uid, type, tid, result);
			return new Response(result);

		} catch (CFException e) {
			return new Response(e);

		} catch (Exception e) {
			e.printStackTrace();
			return new Response(FailCode.APP);
		}
	}
    
    @ApiOperation(value = "创建家族提示接口",httpMethod="POST")
    @ResponseBody
    @RequestMapping("/create_family_tips")
	public Response createFamilyTips(HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		String create_family_tips = null;
		try {
			create_family_tips = tipsService.getCreateFamily();

		} catch (Exception e) {
			e.printStackTrace();
			return new Response(FailCode.APP);
		}
		result.put("create_family_tips", create_family_tips);
		result.put("leader_Incom", "族长每天能获得家族成员贡献收益的30%");

		return new Response(result);
	}
    
    /**
     * 屏蔽家族消息
	 * api/family/shield
	 * shield 是否屏蔽 0:否 1：是
	 * tid 
     * @param request
     * @return
     */
    @ApiOperation(value = "屏蔽家族消息",httpMethod="POST")
   	@ApiImplicitParams({
   		    @ApiImplicitParam(name = "shield", value = "是否屏蔽 0:否 1：是", required =true,  paramType="form",dataTypeClass=Integer.class),
   		    @ApiImplicitParam(name = "tid", value = "家族tid", required =true,  paramType="form",dataTypeClass=String.class)
   	})
    @ResponseBody
    @RequestMapping("/shield")
	public Response shield(HttpServletRequest request,
			@RequestParam(value = "shield", required = true)Integer shield,
			@RequestParam(value = "tid", required = true) String tid) {
    	RedisLockUtil lockUtil = null;
		try {
			Long uid = getUidByToken(request);
			lockUtil = cacheService.getLock(CacheService.KEY_PREFIX.familyMember_.name()+ uid);
			Map<String, Object> result = familyMemberService.shieldFamily(uid, tid, shield);
			log.info("屏蔽家族消息======>uid={},tid={},shield={},result={}", uid, tid, shield,result);
			return new Response(result);

		} catch (CFException e) {
			return new Response(e);

		} catch (Exception e) {
			e.printStackTrace();
			return new Response(FailCode.APP);
		}finally{
			if(lockUtil!=null){
				lockUtil.unlock();
			}
		}
	}
    
    /**
     * 家族成员信息
	 * api/family/member
     * @param tid 
     * @return
     */
    @ApiOperation(value = "查询家族成员信息",httpMethod="POST")
   	@ApiImplicitParams({
   		    @ApiImplicitParam(name = "tid", value = "家族tid", required =true,  paramType="form",dataTypeClass=String.class)
   	})
    @ResponseBody
    @RequestMapping("/member")
	public Response getMember(HttpServletRequest request,
			@RequestParam(value = "tid", required = true) String tid) {
		try {
			Long uid = getUidByToken(request);
			Map<String, Object> result = familyMemberService.getMember(uid, tid);
			return new Response(result);

		} catch (CFException e) {
			e.printStackTrace();
			return new Response(e);

		} catch (Exception e) {
			e.printStackTrace();
			return new Response(FailCode.APP);
		}
	}
    
    
    /**
     * 
     * 更新会话时间
     * /api/family/updateMsgTime
     * @param tid
     * @return
     */
    @ApiOperation(value = "更新会话时间",httpMethod="POST")
   	@ApiImplicitParams({
   		    @ApiImplicitParam(name = "tid", value = "家族tid", required =true,  paramType="form",dataTypeClass=String.class)
   	})
    @ResponseBody
    @RequestMapping("updateMsgTime")
	public Response updateMsgTime(HttpServletRequest request,
			@RequestParam(value = "tid", required = true) String tid) {
		try {
			Long uid = getUidByToken(request);
			Map<String, Object> result = familyMemberService.updateMsgTime(uid, tid);
			return new Response(result);

		} catch (CFException e) {
			e.printStackTrace();
			return new Response(e);

		} catch (Exception e) {
			e.printStackTrace();
			return new Response(FailCode.APP);
		}
	}
    
    /**
     * 查询会话时间
     * /api/family/getMsgTime
     * @param tid
     * @return
     */
    @ApiOperation(value = "查询会话时间",httpMethod="POST")
   	@ApiImplicitParams({
   		    @ApiImplicitParam(name = "tid", value = "家族tid", required =true, paramType="form", dataTypeClass=String.class)
   	})
    @ResponseBody
    @RequestMapping("getMsgTime")
	public Response getMsgTime(HttpServletRequest request,@RequestParam(value = "tid", required = true) String tid) {
		try {
			Long uid = getUidByToken(request);
			Map<String, Object> result = familyMemberService.getMsgTime(uid, tid);
			return new Response(result);

		} catch (CFException e) {
			e.printStackTrace();
			return new Response(e);

		} catch (Exception e) {
			e.printStackTrace();
			return new Response(FailCode.APP);
		}
	}
    
    
    
}
